package org.xhnews.ui;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.RadioButtonField;
import net.rim.device.api.ui.component.RadioButtonGroup;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

import org.xhnews.bo.RssBo;
import org.xhnews.constant.Captions;
import org.xhnews.notifications.EventManager;
import org.xhnews.to.RssChannel;

public class AppEdtRssChnnlPopupScreen extends PopupScreen {
	private ScreenClosedListener listener;
	public AppEdtRssChnnlPopupScreen(ScreenClosedListener listener) {
		super(new VerticalFieldManager(), Field.FOCUSABLE);
		this.listener = listener;
		final EditField title = new EditField("标题：", "", 26,
				EditField.NO_NEWLINE);
		add(title);
		final EditField keywordContain = new EditField("包含关键字：", "", 20,
				EditField.NO_NEWLINE);
		add(keywordContain);
		final EditField keywordNotContain = new EditField("不含关键字：", "", 20,
				EditField.NO_NEWLINE);
		add(keywordNotContain);
		HorizontalFieldManager radios = new HorizontalFieldManager();
		LabelField field = new LabelField("搜索范围：");
		radios.add(field);
		final RadioButtonGroup searchRegion = new RadioButtonGroup();
		radios.add(new RadioButtonField(Captions.rss_searchregion_incontent,
				searchRegion, true));
		radios.add(new RadioButtonField(Captions.rss_searchregion_intitle,
				searchRegion, false));
		add(radios);
		add(new SeparatorField());
		FieldChangeListener sbcl = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				RssChannel newRssChannel = RssBo.makeRssChannelObject(title
						.getText(), keywordContain.getText(), keywordNotContain
						.getText(), searchRegion.getSelectedIndex());
				RssBo.appendRssChannel(newRssChannel);
				close();
			}
		};
		FieldChangeListener cbcl = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				close();
			}
		};
		HorizontalFieldManager buttons = new HorizontalFieldManager(
				Field.FIELD_HCENTER | Field.USE_ALL_WIDTH);
		ButtonField submitButton = new ButtonField("确认");
		buttons.add(submitButton);
		submitButton.setChangeListener(sbcl);
		ButtonField cancelButton = new ButtonField("取消");
		buttons.add(cancelButton);
		cancelButton.setChangeListener(cbcl);
		add(buttons);
	}

	public AppEdtRssChnnlPopupScreen(final RssChannel rsschannel,ScreenClosedListener listener) {
		super(new VerticalFieldManager(), Field.FOCUSABLE);
		this.listener = listener;
		final EditField title = new EditField("标题：", rsschannel.getName(), 26,
				EditField.NO_NEWLINE);
		add(title);
		final EditField keywordContain = new EditField("包含关键字：", rsschannel
				.getKeycontain(), 20, EditField.NO_NEWLINE);
		add(keywordContain);
		final EditField keywordNotContain = new EditField("不含关键字：", rsschannel
				.getKeynotcontain(), 20, EditField.NO_NEWLINE);
		add(keywordNotContain);
		HorizontalFieldManager radios = new HorizontalFieldManager();
		LabelField field = new LabelField("搜索范围：");
		radios.add(field);
		final RadioButtonGroup searchRegion = new RadioButtonGroup();
		radios.add(new RadioButtonField(Captions.rss_searchregion_incontent,
				searchRegion, false));
		radios.add(new RadioButtonField(Captions.rss_searchregion_intitle,
				searchRegion, false));
		searchRegion.setSelectedIndex(rsschannel.getSearchregion());
		add(radios);
		add(new SeparatorField());
		FieldChangeListener sbcl = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				RssChannel newRssChannel = RssBo.makeRssChannelObject(title
						.getText(), keywordContain.getText(), keywordNotContain
						.getText(), searchRegion.getSelectedIndex());
				newRssChannel.setId(rsschannel.getId());
				RssBo.updateRssChannel(newRssChannel);
				close();
			}
		};
		FieldChangeListener cbcl = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				close();
			}
		};
		HorizontalFieldManager buttons = new HorizontalFieldManager(
				Field.FIELD_HCENTER | Field.USE_ALL_WIDTH);
		ButtonField submitButton = new ButtonField("确认");
		buttons.add(submitButton);
		submitButton.setChangeListener(sbcl);
		ButtonField cancelButton = new ButtonField("取消");
		buttons.add(cancelButton);
		cancelButton.setChangeListener(cbcl);
		add(buttons);
	}
	protected void onUndisplay() {
	       listener.notifyScreenClosed(this);
	    } 
	
}
